package com.example.taobaounion.ui.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.taobaounion.R;
import com.example.taobaounion.model.bean.SelectedPageCategory;

import java.util.ArrayList;
import java.util.List;

public class SelectedPageLeftAdapter extends RecyclerView.Adapter<SelectedPageLeftAdapter.InnerHolder> {

    private List<SelectedPageCategory.DataBean> mData=new ArrayList<>();
    private int mCurrentSelectedPosition=0;
    private OnLeftItemClickListener mItemClickListener =null;

    @Override
    public InnerHolder onCreateViewHolder(@NonNull  ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_selected_page_left, parent, false);

        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull  SelectedPageLeftAdapter.InnerHolder holder, int position) {
        SelectedPageCategory.DataBean dataBean = mData.get(position);
        TextView itemTv = holder.itemView.findViewById(R.id.left_category_tv);
        itemTv.setText(dataBean.getFavorites_title());
        if(mCurrentSelectedPosition==position){
            itemTv.setBackgroundColor(itemTv.getResources().getColor(R.color.color_page_Bg));
        }else {
            itemTv.setBackgroundColor(Color.parseColor("#ffffff"));
        }
        itemTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemClickListener != null&&mCurrentSelectedPosition!=position) {
                    //修改当前选中的位置
                    mCurrentSelectedPosition=position;
                    //接口回调
                    mItemClickListener.onLeftItemClick(dataBean);
                    notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    //设置数据
    public void setData(SelectedPageCategory categories) {
        List<SelectedPageCategory.DataBean> data = categories.getData();
        if(data!=null){
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        }
        //默认选中第一个页面的分类详情
        if(mData.size()>0){
            mItemClickListener.onLeftItemClick(data.get(mCurrentSelectedPosition));
        }

    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        public InnerHolder(@NonNull  View itemView) {
            super(itemView);
        }
    }
    public void setOnLeftItemClickListener(OnLeftItemClickListener listener){
        this.mItemClickListener =listener;
    }
    public interface OnLeftItemClickListener{
        void onLeftItemClick(SelectedPageCategory.DataBean data);
    }
}
